package com.sxyjhh.utils.util;

import javax.servlet.http.HttpServletRequest;

/**
 * Description:网络相关方法封装类
 *
 * @JDK Version:1.8.0_40
 * Version: V1.0
 * Author: lsk
 * Date: 2016/3/22 0022 15:35
 */
public class NetUtil {
    public static final String LOCAL_IP_127_0_0_1 = "127.0.0.1";

    /**
     * Descreption：获取IP地址
     * version:V1.0
     * Author：lsk
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
